package com.ruoyi.demo.domain.wrapper.view;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.eic.framework.core.domain.BaseEntityWrapperV;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.demo.domain.dao.DemoDept;
import com.ruoyi.demo.domain.dao.DemoUser;
import com.ruoyi.demo.domain.view.DemoUserVo;
import com.ruoyi.demo.service.IDemoDeptService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 人员前端实体包装
 *
 * @author 尹国春
 * @date 2022-02-25
 */
public class DemoUserWrapperV extends BaseEntityWrapperV<DemoUser, DemoUserVo> {

    private static DemoUserWrapperV wrapperV=null;
    //示例代码
    private static IDemoDeptService demoDeptService;
    private DemoUserWrapperV(){
    }

    public static DemoUserWrapperV build(){
        if(wrapperV==null){
            //示例代码
            demoDeptService= SpringUtils.getBean(IDemoDeptService.class);
            Assert.notNull(demoDeptService);
            wrapperV=new DemoUserWrapperV();
        }
        return wrapperV;
    }

    @Override
    public DemoUserVo entityVO(DemoUser entity) {
        if(entity==null){
            return new DemoUserVo();
        }
        DemoUserVo vo = BeanUtil.copyProperties(entity,DemoUserVo.class);
        vo.setStateName(DictUtils.getDictLabel("sys_normal_disable",entity.getState()));
        //示例代码
        vo.setDeptName(getFieldValue(demoDeptService,entity.getDeptId(), DemoDept::getDepartName));
        return vo;
    }

    @Override
    public List<DemoUserVo> listVO(List<DemoUser> list) {
        List<DemoUserVo> voList=list.stream().map(user -> BeanUtil.copyProperties(user,DemoUserVo.class)).collect(Collectors.toList());
        copyForeignKeyData(voList,DemoUserVo::getDeptId,DemoUserVo::setDeptName,demoDeptService,DemoDept::getDepartName);
        voList.forEach(vo -> vo.setStateName(DictUtils.getDictLabel("sys_normal_disable",vo.getState())));
        return voList;
    }

}